import { login, getProfile, getBasicUserInfo } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || null,
    userInfo: {}
  },
  getters: {

  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)// 调用封装好的存储cookie方法
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
      state.userInfo.staffPhoto = 'https://img2.baidu.com/it/u=3094149767,177600321&fm=253&app=138&size=w931&n=0&f=JPEG'
    }
  },
  actions: {
    async userLogin(context, newData) {
      const res = await login(newData)
      context.commit('setToken', res.data)
    },
    async getUserInfo(context) {
      const res = await getProfile()
      const id = res.data.userId
      const res2 = await getBasicUserInfo(id)
      context.commit('setUserInfo', { ...res.data, ...res2.data })
    },
    async userLogOut(context) {
      context.commit('setToken', '')
      context.commit('setUserInfo', {})
      resetRouter()
    }
  },
  modules: {

  }
}

